package com.spzx.product.component;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.product.domain.ProductSku;
import com.spzx.product.mapper.ProductSkuMapper;
import jakarta.annotation.Resource;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

// 使用@Component 注册后,应用初始化后就会执行任务
@Component
public class InitSkuBloomFilter implements CommandLineRunner {

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private ProductSkuMapper productSkuMapper;

    @Override
    public void run(String... args) throws Exception {
        //通过queryWrapper的组装吗，只获取ProductSku中的skuId列表
        LambdaQueryWrapper<ProductSku> queryWrapper = new LambdaQueryWrapper<ProductSku>()
                .select(ProductSku::getId)
                .eq(ProductSku::getStatus, 1);
        List<Object> productSkuIdList = productSkuMapper.selectObjs(queryWrapper);

        // 初始化时将sku加入到bitmap
        String key = "product:sku:data";
        for (Object id : productSkuIdList) {
            System.out.println("id = " + id);
            redisTemplate.opsForValue().setBit(key, (Long) id, true);
        }
    }
}
